package cc.rengu.igas.channel.cups.core.service.trans;

import cc.rengu.igas.channel.cups.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.cups.core.service.base.ManageService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.SrcChannelInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.SrcChannelInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.SrcChannelInfo;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSON;

/**
 * 银联发起关闭发卡侧机构
 */
public class AcpCupsCloseRcvInstService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        return null;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse bizResponse = new BizResponse();
        /*更新通道状态*/
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        String channelKey = instId.trim() + ChannelEnum.CUPS.getChannelType() + "*";
        SrcChannelInfo srcChannelInfo = JSON.parseObject(RedisUtil.hashGet(AppParamConstant.CHANNEL_ACQ_INFO_CACHE, channelKey), SrcChannelInfo.class);
        if (null == srcChannelInfo) {
            rglog.error("获取<{}>作为渠道侧接入的配置信息失败!", ChannelEnum.CUPS.getChannelType());
            throw new BizException(RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespCode(), RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespDesc());
        }
        /* 更新Redis缓存 */
        srcChannelInfo.setTransChannelStatus(AppParamConstant.NO);
        RedisUtil.hashSet(AppParamConstant.CHANNEL_ACQ_INFO_CACHE, channelKey, JSON.toJSONString(srcChannelInfo));
        /* 更新数据库状态 */
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();
        SrcChannelInfoMapper srcChannelInfoMapper = new SrcChannelInfoMapperImpl();
        int iReturnCode = srcChannelInfoMapper.updateSrcChannelInfoByPrimaryKey(srcChannelInfo);
        if (0 != iReturnCode) {
            rglog.error("更新<{}>作为渠道侧接入的配置信息失败!", ChannelEnum.CUPS.getChannelType());
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespCode(), RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespDesc());
        }
        dbsUtil.dbsEndTransaction(true);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {

    }
}
